#coding=utf-8

import urllib
import urllib2
import cookielib
import json
import random
import sys
from django.http import HttpResponse


def getRestList(offset,longitude,latitude):
	print '爬取 '+str(longitude)+','+str(latitude)+' 餐厅,目标数据 ' + str(offset) + ' - ' + str(offset+30)
	url 		=	'https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=activities&geohash=wtw3evq7uss&latitude='+str(latitude)+'&limit=30&longitude='+str(longitude)+'&sign='+str(1506307800000+random.randint(0,9999))+'&terminal=web&offset=' + str(offset)
	print url
	userAgent	= 	'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'
	headers		=	{
						'User-Agent':userAgent,
						'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
					}
	data		=	urllib.urlencode({})
	request 	=	urllib2.Request(url+'?'+data,headers=headers)
	restList 	=	[]
	try:
		response 	= 	urllib2.urlopen(request)
		body = response.read().decode('utf-8');
		body = json.loads(body)
		for restaurant in body:
			restList += [restaurant['name']]
	except urllib2.URLError, e:
		print 'error - %d' % (e.code)

	return restList


# 去重
def repeatScreen(allList):
	tempList=[]
	for name in allList:
		name+="（("
		name=name[0:min(name.index("（"),name.index("("))]
		tempList+=[name]#把店名()删掉
	return [i for i in set(tempList)]

# 黑名单
def removeBlock(allList,blacklist):
	tempList=[]
	for name in allList:
		isBlack = False
		for blackkey in blacklist:
			if name.find(blackkey) != -1:
				isBlack=True
				break
		if isBlack:
			continue
		tempList+=[name]
	return tempList

def getRandomList(request):
	latitude  = request.GET.get("lat")
	longitude = request.GET.get("lon")
	print latitude
	print longitude

	reload(sys)
	sys.setdefaultencoding('utf8')
	print sys.version   
	print sys.version_info  

	# #每次最多查询30条数据
	count = 30*5 
	allList = []
	while len(allList) < count:
		allList += getRestList(len(allList),121.46264,31.23068)

	len1 = len(allList)
	print '总数\t'+str(len1)

	allList = repeatScreen(allList)
	len1 = len(allList)
	print '去重后\t'+str(len1)

	blacklist = ["粥","沙拉","寿司","油条","包子","馒头","点心","葳姐","饼"]
	allList = removeBlock(allList,blacklist)
	len1 = len(allList)
	print '黑名单屏蔽后\t'+str(len1)
	# for name in allList:
	# 	print name.encode("utf-8")


	responseList = []
	for index in range(0,5):
		len1 = len(allList)
		randomIndex = random.randint(0,len1-1)
		name = (allList[randomIndex]).encode('utf-8')
		del allList[randomIndex]
		responseList += ["随机种子:" + str(randomIndex) + "\t\tres : " + name]


	return HttpResponse(json.dumps({"responseList":responseList}), content_type="application/json")





